﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Wjtour.Common;

namespace Wjtour.BLL
{
    public class slides_bll : base_dll
    {
        public List<Wjtour.Domain.slides> GetList(int cid)
        {
            string where = " where 1=1";
            if (cid > 0)
                where += " and a.cid = " + cid;
            string sql = string.Format(@"select * from slides a
                {0} 
                order by sort desc,id asc", where);
            return DbAccess.GetObjects<Wjtour.Domain.slides>(sql).ToList();
        }

        public PaginationData<Wjtour.Domain.slides> GetList(string q, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(q))
                where += " and a.title like '%" + Utility.GetSafeSql(q) + "%' ";
            string sql = string.Format(@"select * from slides a  
                {0} 
                order by id desc", where);
            PaginationData<Wjtour.Domain.slides> data = GetPaginationData<Wjtour.Domain.slides>(sql, page, pagesize);
            return data;
        }

        public Wjtour.Domain.slides Load(int id)
        {
            string sql = "select * from slides where id=" + id;
            return DbAccess.GetObjects<Wjtour.Domain.slides>(sql).FirstOrDefault();
        }

        public bool Save(Wjtour.Domain.slides entity)
        {
            string sql = "";

            DbCommand cmd = DbAccess.CreateCommand();

            if (entity.id > 0)
            {
                sql = @"UPDATE slides SET 
                img = @img,
                url = @url,
                title = @title,
                hide = @hide,
                addtime = @addtime,
                sort = @sort,
                cid = @cid
                 WHERE id=@id";
                DbAccess.AddInParameter(cmd, "@id", DbType.Int32, entity.id);

            }
            else
            {
                sql = @"INSERT INTO slides( 
                    img,url,title,hide,addtime,sort,cid)VALUES(
                    @img,@url,@title,@hide,@addtime,@sort,@cid)";
            }
            DbAccess.AddInParameter(cmd, "@img", DbType.String, entity.img);
            DbAccess.AddInParameter(cmd, "@url", DbType.String, entity.url);
            DbAccess.AddInParameter(cmd, "@title", DbType.String, entity.title);
            DbAccess.AddInParameter(cmd, "@hide", DbType.Int32, entity.hide);
            DbAccess.AddInParameter(cmd, "@addtime", DbType.DateTime, entity.addtime);
            DbAccess.AddInParameter(cmd, "@sort", DbType.Int32, entity.sort);
            DbAccess.AddInParameter(cmd, "@cid", DbType.Int32, entity.cid);
            cmd.CommandText = sql;
            cmd.CommandType = System.Data.CommandType.Text;
            return DbAccess.ExecuteNonQuery(cmd) > 0;

        }

        public int BatchDelete(string ids)
        {
            string sql = "delete from slides where id in(" + ids + ")";
            return DbAccess.ExecuteNonQuery(sql);
        }
    }
}
